<?php
namespace app\plus\controller;

use think\Controller;
use think\Db;

class AjaxController extends Controller
{
    
    /**
     * 分页选择
    */
    public function selectpage()
    {
        $pageNumber = $this->request->param('pageNumber/d'); //当前页数
        $pageSize = $this->request->param('pageSize/d'); //每页记录数
        $orderBy = $this->request->param('orderBy/a');
        $searchTable = $this->request->param('searchTable/s');
        $searchField = $this->request->param('searchField/s');
        $q_word = $this->request->param('q_word/a'); //多个关键词结合andOr检索
        $searchKey = $this->request->param('searchKey/s'); //查询初始化数据
        $searchValue = $this->request->param('searchValue/s'); //查询初始化数据
        try{
            //处理排序
            if(!empty($orderBy)){
                foreach($orderBy as $v){
                    $order[] = "{$v[0]} $v[1]";
                }
                $order = implode(',',$order);
            }
            //处理模糊多关键词检索搜索
            $map=[];
            if(!empty($q_word)){
                $query=[];
                foreach($q_word as $v){
                    $query[]='%'.$v.'%';
                }
                $map[$searchField] = ['like',$query,'OR'];
            }
            //处理默认选中
            if(!empty($searchKey) && !empty($searchValue)){
                $map[$searchKey] = ['in',$searchValue];
            }
            switch ($searchTable){
                case 'car_btype': //客户表自定义
                    $btype_type = $this->request->param('btype_type/d',-1);
                    if($btype_type>-1){
                        $map['btype_type'] = $btype_type;
                    }
                    break;
            }
            $data['totalRow']=Db::table($searchTable)->where($map)->count();
            $data['list']=Db::table($searchTable)->where($map)->page($pageNumber,$pageSize)->order($order)->select();
            $data['sql']=Db::table($searchTable)->getLastSql();
        } catch (\Exception $e){
            echo 'server error:'.$e->getMessage();
        }
        return $data;
    }

}